import 'package:flutter/material.dart';

import 'page_notifier.dart';

/// FileName tab_notifier
///
/// @Author lax
/// @Date 2023/5/10
///
/// @Description

class PageViewNotifier extends PageNotifier {
  /// 初始化页面 默认第一页
  final int? initialPage;

  late PageController controller;

  /// 当前页面
  int get currentPage {
    if (controller.positions.length == 1) {
      return controller.page?.round() ?? 0;
    }
    return 0;
  }

  PageViewNotifier({this.initialPage}) {
    controller = PageController(initialPage: initialPage ?? 0);
  }

  /// 跳转上一页
  void jumpPreviousPage({bool animate = true}) {
    if (animate) {
      controller.previousPage(
          duration: const Duration(milliseconds: 200), curve: Curves.ease);
    } else {
      controller.jumpToPage(currentPage - 1);
    }
  }

  /// 跳转下一页
  void jumpNextPage({bool animate = true}) {
    if (animate) {
      controller.nextPage(
          duration: const Duration(milliseconds: 200), curve: Curves.ease);
    } else {
      controller.jumpToPage(currentPage + 1);
    }
  }
}
